﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class EmailLog
    {
        public int Add(Chain.Model.EmailLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into EmailLog(");
            builder.Append("EmailAdress,EmailTitle,EmailContent,EmailState,EmailSendTime,EmailShopID,EmailUserID,EmailCount)");
            builder.Append(" values (");
            builder.Append("@EmailAdress,@EmailTitle,@EmailContent,@EmailState,@EmailSendTime,@EmailShopID,@EmailUserID,@EmailCount)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailAdress", SqlDbType.NVarChar, 100), new SqlParameter("@EmailTitle", SqlDbType.NVarChar, 500), new SqlParameter("@EmailContent", SqlDbType.NVarChar, 500), new SqlParameter("@EmailState", SqlDbType.Int, 4), new SqlParameter("@EmailSendTime", SqlDbType.DateTime), new SqlParameter("@EmailShopID", SqlDbType.Int, 4), new SqlParameter("@EmailUserID", SqlDbType.Int, 4), new SqlParameter("@EmailCount", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.EmailAdress;
            cmdParms[1].Value = model.EmailTitle;
            cmdParms[2].Value = model.EmailContent;
            cmdParms[3].Value = model.EmailState;
            cmdParms[4].Value = model.EmailSendTime;
            cmdParms[5].Value = model.EmailShopID;
            cmdParms[6].Value = model.EmailUserID;
            cmdParms[7].Value = model.EmailCount;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int EmailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from EmailLog ");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = EmailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string EmailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from EmailLog ");
            builder.Append(" where EmailID in (" + EmailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public int EmailResend(int intEamilID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" Update EmailLog set EmailState=0, ");
            builder.Append(" EmailCount=0 ");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = intEamilID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public bool Exists(int EmailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from EmailLog");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = EmailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select EmailID,EmailAdress,EmailTitle,EmailContent,EmailState,EmailSendTime,EmailShopID,EmailUserID,EmailCount ");
            builder.Append(" FROM EmailLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" EmailID,EmailAdress,EmailTitle,EmailContent,EmailState,EmailSendTime,EmailShopID,EmailUserID,EmailCount ");
            builder.Append(" FROM EmailLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.EmailID desc");
            }
            builder.Append(")AS Row, T.*  from EmailLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysShop,EmailLog,SysUser";
            string[] column = new string[] { "SysShop.ShopName,EmailLog.*,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "EmailID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("EmailID", "EmailLog");
        }

        public Chain.Model.EmailLog GetModel(int EmailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 EmailID,EmailAdress,EmailTitle,EmailContent,EmailState,EmailSendTime,EmailShopID,EmailUserID,EmailCount from EmailLog ");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = EmailID;
            Chain.Model.EmailLog log = new Chain.Model.EmailLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["EmailID"] != null) && (set.Tables[0].Rows[0]["EmailID"].ToString() != ""))
            {
                log.EmailID = int.Parse(set.Tables[0].Rows[0]["EmailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EmailAdress"] != null) && (set.Tables[0].Rows[0]["EmailAdress"].ToString() != ""))
            {
                log.EmailAdress = set.Tables[0].Rows[0]["EmailAdress"].ToString();
            }
            if ((set.Tables[0].Rows[0]["EmailTitle"] != null) && (set.Tables[0].Rows[0]["EmailTitle"].ToString() != ""))
            {
                log.EmailTitle = set.Tables[0].Rows[0]["EmailTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["EmailContent"] != null) && (set.Tables[0].Rows[0]["EmailContent"].ToString() != ""))
            {
                log.EmailContent = set.Tables[0].Rows[0]["EmailContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["EmailState"] != null) && (set.Tables[0].Rows[0]["EmailState"].ToString() != ""))
            {
                log.EmailState = int.Parse(set.Tables[0].Rows[0]["EmailState"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EmailSendTime"] != null) && (set.Tables[0].Rows[0]["EmailSendTime"].ToString() != ""))
            {
                log.EmailSendTime = DateTime.Parse(set.Tables[0].Rows[0]["EmailSendTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EmailShopID"] != null) && (set.Tables[0].Rows[0]["EmailShopID"].ToString() != ""))
            {
                log.EmailShopID = int.Parse(set.Tables[0].Rows[0]["EmailShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EmailUserID"] != null) && (set.Tables[0].Rows[0]["EmailUserID"].ToString() != ""))
            {
                log.EmailUserID = int.Parse(set.Tables[0].Rows[0]["EmailUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EmailCount"] != null) && (set.Tables[0].Rows[0]["EmailCount"].ToString() != ""))
            {
                log.EmailCount = int.Parse(set.Tables[0].Rows[0]["EmailCount"].ToString());
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM EmailLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.EmailLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update EmailLog set ");
            builder.Append("EmailAdress=@EmailAdress,");
            builder.Append("EmailTitle=@EmailTitle,");
            builder.Append("EmailContent=@EmailContent,");
            builder.Append("EmailState=@EmailState,");
            builder.Append("EmailSendTime=@EmailSendTime,");
            builder.Append("EmailShopID=@EmailShopID,");
            builder.Append("EmailUserID=@EmailUserID,");
            builder.Append("EmailCount=@EmailCount");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailAdress", SqlDbType.NVarChar, 100), new SqlParameter("@EmailTitle", SqlDbType.NVarChar, 500), new SqlParameter("@EmailContent", SqlDbType.NVarChar, 500), new SqlParameter("@EmailState", SqlDbType.Int, 4), new SqlParameter("@EmailSendTime", SqlDbType.DateTime), new SqlParameter("@EmailShopID", SqlDbType.Int, 4), new SqlParameter("@EmailUserID", SqlDbType.Int, 4), new SqlParameter("@EmailCount", SqlDbType.Int, 4), new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.EmailAdress;
            cmdParms[1].Value = model.EmailTitle;
            cmdParms[2].Value = model.EmailContent;
            cmdParms[3].Value = model.EmailState;
            cmdParms[4].Value = model.EmailSendTime;
            cmdParms[5].Value = model.EmailShopID;
            cmdParms[6].Value = model.EmailUserID;
            cmdParms[7].Value = model.EmailCount;
            cmdParms[8].Value = model.EmailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateEmail(int intID, int state)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" Update EmailLog set EmailState=@EmailState ");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailState", SqlDbType.Int, 4), new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = state;
            cmdParms[1].Value = intID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public int UpdateEmailCount(int intID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" Update EmailLog set EmailCount=EmailCount+1 ");
            builder.Append(" where EmailID=@EmailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@EmailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = intID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

